﻿using MediatR;
using Rabbit.Domain;
using Rabbit.ErCode;
using Rabbit.lnfarnum;
using Rabbit.Write.API.WriteRepository.Commder;

namespace Rabbit.Write.API.WriteRepository.CommdHnader
{
    public class SpDeleteCommdHnader : IRequestHandler<SpDeleteCommdes, APIResult<object>>
    {

        private readonly IBaseRepository<MerchandiseMangem> baseRepository;


        public SpDeleteCommdHnader(IBaseRepository<MerchandiseMangem> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        public async Task<APIResult<object>> Handle(SpDeleteCommdes request, CancellationToken cancellationToken)
        {
            var result = new APIResult<object>();

            if(request.Ids==null || !request.Ids.Any())
            {
                result.Code = ReCode.删除失败;
                result.Mage = "选择要删除数据";
                return result;
            }

            var entities = baseRepository.GetAll().Where(x=>request.Ids.Contains(x.MerchanMid))
                .ToList();

            if(!entities.Any() )
            {
                result.Code = ReCode.删除失败;
                result.Mage = "未找到要删的记录";
                return result;
            }
            entities.ForEach(e => e.Idate = true);

            var affectedRows =await baseRepository.DeleteRemove(entities);

            if(affectedRows>0)
            {
                result.Code = ReCode.删除失败;
                result.Mage = "批量删除成功";
      
                return result;
            }

            return await Task.FromResult(result);
        }
    }
}
